package com.aizuda.boot.modules.business.approval.controller;

import com.aizuda.boot.modules.business.approval.entity.dto.TaskCenterQueryDTO;
import com.aizuda.boot.modules.business.approval.entity.vo.PendingApprovalTaskVO;
import com.aizuda.boot.modules.business.approval.service.TaskCenterService;
import com.aizuda.boot.modules.business.contract.util.AuthUtil;
import com.aizuda.boot.modules.flw.entity.vo.TaskApprovalVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotNull;

/**
 * 任务中心控制器
 */
@Tag(name = "任务中心")
@Slf4j
@RestController
@RequestMapping("/taskCenter")
@RequiredArgsConstructor
public class TaskCenterController {

    private final TaskCenterService taskCenterService;

    @Operation(summary = "获取待审批任务列表")
    @PostMapping("/pending")
    public Page<PendingApprovalTaskVO> getPendingApprovalTasks(@RequestBody TaskCenterQueryDTO queryDTO) {

        Long userId = AuthUtil.getCurrentUser().getId();
        log.info("获取待审批任务列表，用户ID: {}, 查询参数: {}", userId, queryDTO);

        // 使用优化版本的方法
        Page<PendingApprovalTaskVO> result = taskCenterService.getPendingApprovalTasksOptimized(queryDTO, userId);
        return result;
    }

    @Operation(summary = "获取已处理任务列表")
    @PostMapping("/processed")
    public Page<PendingApprovalTaskVO> getProcessedTasks(@RequestBody TaskCenterQueryDTO queryDTO) {

        Long userId = AuthUtil.getCurrentUser().getId();
        log.info("获取已处理任务列表，用户ID: {}, 查询参数: {}", userId, queryDTO);

        // 使用优化版本的方法
        Page<PendingApprovalTaskVO> result = taskCenterService.getProcessedTasksOptimized(queryDTO, userId);
        return result;
    }

    @Operation(summary = "获取抄送任务列表")
    @PostMapping("/cc")
    public Page<PendingApprovalTaskVO> getCcTasks(@RequestBody TaskCenterQueryDTO queryDTO) {

        Long userId = AuthUtil.getCurrentUser().getId();
        log.info("获取抄送任务列表，用户ID: {}, 查询参数: {}", userId, queryDTO);

        // 使用优化版本的方法
        Page<PendingApprovalTaskVO> result = taskCenterService.getCcTasksOptimized(queryDTO, userId);
        return result;
    }

    @Operation(summary = "根据主实例ID和子流程节点获取子流程审批信息")
    @PostMapping("/sub-process-approval-info")
    public TaskApprovalVO getSubProcessApprovalInfo(
            @Parameter(description = "主流程实例ID", required = true) @RequestParam @NotNull Long mainInstanceId,
            @Parameter(description = "子流程节点的nodeKey", required = true) @RequestParam @NotNull String subProcessNodeKey) {

        log.info("获取子流程审批信息，主实例ID: {}, 子流程节点: {}", mainInstanceId, subProcessNodeKey);

        return taskCenterService.getSubProcessApprovalInfo(mainInstanceId, subProcessNodeKey);
    }
}
